import { _decorator, Component, Node, Prefab, Vec3, instantiate, randomRange} from 'cc';
const { ccclass, property } = _decorator;

import { CardManager } from './CardManager';
import { Card } from './Card';

@ccclass('DeckCardManager') // 用 @ccclass 声明类为 Cocos 组件
export class DeckCardManager extends Component {

    @property([Node]) decks: Node[] = [];
    @property(Prefab) cardPerfab = null;

	static instance: DeckCardManager;

    private startIndex = 0;
    private currentIndex = 0;
    private sideCount = 4;
    private sideLength = 17;
    private sideStack = 2;


    private inited = false;

    onLoad() {
        DeckCardManager.instance = this;
        console.log('DeckCardManager onLoad!');
    }

    start() {
        // 脚本启动时的逻辑
    }

    
    initWholeDeck() {
        
        const baseRise = 0.000;
        const cardRise = 0.02;

        const count = this.sideCount;
        for(let i = 0; i < count; i++) {

            let node = this.decks[i];
            node.removeAllChildren();

            const length = this.sideLength;
            for(let j = 0; j < length; j++){
                for(let k = 0; k < 2; k++){
                    const model = this.createSimpleCard();
                    if(model)
                    {
                        const card: Node = model
                        card.position = new Vec3(j*0.0276, baseRise + k * cardRise, 0);
                        card.scale = new Vec3(0.92, 0.92, 0.92);
                        node.addChild(card);
                    }
                }
            }
        }

        //hide for animation
        this.node.scale = new Vec3(0.0, 0.0, 0.0);

        this.inited = true;
    }

    createSimpleCard() {
        // const card = instantiate(this.cardPerfab);
        // return card;

        const cardNode = CardManager.createDeskCard();
        return cardNode;
    }

    setStartPos() {

        if(this.inited == false)
        {
            return;
        }

        this.startIndex = Math.floor(randomRange(0, this.sideLength * this.sideCount)) * this.sideStack;
        this.currentIndex = this.startIndex;
        console.log("setStartPos...", this.startIndex);

        let count = 12;
        for(let i = 0; i < count; i++) {
            // this.RemoveCard(4);
            this.scheduleOnce(this.RemoveCard.bind(this, 4), i*0.13);
        }

        this.scheduleOnce(this.setGrayDeck, 1.8);
    }

    RemoveCurrentCard() {
        let sideCards = this.sideLength * 2;
        let serialNumber = this.currentIndex;
        let stack = Math.floor( serialNumber / sideCards ) ;
        let index = Math.floor( serialNumber % sideCards ) ;
        // console.log("RemoveCurrentCard...", stack, index);
        let nodeList = this.decks[stack];
        let node = nodeList.children[index];
        
        if(node)
        {
            // node.removeFromParent();
            // node.destroy();
            node.active = false;
            
            this.currentIndex++;
            if(this.currentIndex >= 136)
            {
                this.currentIndex = 0;
            }
        }
            
        
    }

    RemoveCard(count:number) {

        for(let i = 0; i < count; i++) {
            this.RemoveCurrentCard();
        }
    }

    setGrayDeck() {

        const max = 20;
        let grayLib = [];
        for(let i = 0; i < max; i++) {
            let index = this.startIndex - i - 1;
            if(index < 0)
                index += this.sideLength * this.sideCount * this.sideStack;
            grayLib.push(index);
        }


        for(let i = 0; i < grayLib.length; i++) {
            
            let serialNumber = grayLib[i];

            let sideCards = this.sideLength * 2;
            let stack = Math.floor( serialNumber / sideCards ) ;
            let index = Math.floor( serialNumber % sideCards ) ;
            // console.log("RemoveCurrentCard...", stack, index);
            let nodeList = this.decks[stack];
            const cardNode = nodeList.children[index];
            let card = cardNode.getComponent(Card);
            if(card)
            {
                card.setDisable();
                // model.layer = 2 << 13;
            }
            
        }
    }

}